package com.atguigu.app;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.otter.canal.client.CanalConnector;
import com.alibaba.otter.canal.client.CanalConnectors;
import com.alibaba.otter.canal.protocol.CanalEntry;
import com.alibaba.otter.canal.protocol.Message;
import com.atguigu.constants.GmallConstants;
import com.atguigu.uitls.MyKafkaSender;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;

import java.net.InetSocketAddress;
import java.util.List;

public class CanalClient {
    public static void main(String[] args) throws InvalidProtocolBufferException {
        //1.获取连接器
        CanalConnector canalConnector = CanalConnectors.newSingleConnector(new InetSocketAddress("hadoop102", 11111), "example", "", "");

        while (true){
            //2.获取链接
            canalConnector.connect();

            //3.选择订阅的数据库
            canalConnector.subscribe("gmall220212.*");

            //4.获取多个sql执行结果
            Message message = canalConnector.get(100);

            //5.获取一个sql执行的结果
            List<CanalEntry.Entry> entries = message.getEntries();

            //6.通过判断list集合中是否有数据，进而判断mysql中是否有数据变化
            if (entries.size()<=0){
                //没有数据变化
                System.out.println("没有数据，休息一会。。。。");
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }else {
                //7.有数据变化,获取到每一个entry
                for (CanalEntry.Entry entry : entries) {
                    //TODO 8.获取表名
                    String tableName = entry.getHeader().getTableName();

                    //9.获取entry类型
                    CanalEntry.EntryType entryType = entry.getEntryType();

                    //10.根据entry类型判断，获取到序列化数据
                    if (CanalEntry.EntryType.ROWDATA.equals(entryType)){

                        //11.取出序列化数据
                        ByteString storeValue = entry.getStoreValue();

                        //12.对数据做反序列化
                        CanalEntry.RowChange rowChange = CanalEntry.RowChange.parseFrom(storeValue);

                        //TODO 13.获取事件类型
                        CanalEntry.EventType eventType = rowChange.getEventType();

                        //TODO 14.获取封装每一行数据的list集合
                        List<CanalEntry.RowData> rowDatasList = rowChange.getRowDatasList();

                        handle(tableName, eventType, rowDatasList);
                    }
                }

            }
        }
    }

    private static void handle(String tableName, CanalEntry.EventType eventType, List<CanalEntry.RowData> rowDatasList) {
        //通过表名和类型来判断获取那张表中哪种类型的数据
        //获取订单表中新增的数据
        if ("order_info".equals(tableName)&&CanalEntry.EventType.INSERT.equals(eventType)){
            //获取每一行的数据
            for (CanalEntry.RowData rowData : rowDatasList) {
                List<CanalEntry.Column> afterColumnsList = rowData.getAfterColumnsList();

                JSONObject jsonObject = new JSONObject();

                //获取到一行中每一列的数据
                for (CanalEntry.Column column : afterColumnsList) {
                    jsonObject.put(column.getName(), column.getValue());
                }

                System.out.println(jsonObject.toJSONString());
                MyKafkaSender.send(GmallConstants.KAFKA_TOPIC_ORDER, jsonObject.toJSONString());
            }
        }
    }
}
